<?php

namespace App\Services;

use App\Models\VolunteerActivity;
use App\Repositories\Contracts\VolunteerActivityRepository;

class VolunteerActivityService
{
    private $volunteerActivityRepository;

    public function __construct(VolunteerActivityRepository $volunteerActivityRepository)
    {
        $this->volunteerActivityRepository = $volunteerActivityRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [
                'keyword' => $param['keyword'] ?? '',
            ];
            $result = $this->volunteerActivityRepository->paginate($param['per_page'] ?? null, $filter);

            // $withArr = VolunteerActivity::withCount(['volunteerForm' => function ($query) {
            //     $query->where('audit_status', '1');
            // }])->paginate($param['per_page']);
            // dd($withArr->toArray());

            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->volunteerActivityRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $volunteerActivity = $this->volunteerActivityRepository->store($param);
            return $volunteerActivity;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $volunteerActivity = $this->volunteerActivityRepository->update($param, $id);
            return $volunteerActivity;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $volunteerActivity = $this->volunteerActivityRepository->delete($id);

            $result = [];
            if ($volunteerActivity) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
